package com.huidz.calendar.mainfragment;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.huidz.calendar.R;
import com.huidz.calendar.activity.HuangliActivity;
import com.huidz.calendar.activity.HuangliXdwActivity;
import com.huidz.calendar.activity.LunchActivity;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.LunarCalendar;
import com.huidz.calendar.util.MyDate;
import com.huidz.calendar.util.TianGanDizhiShengXiao;
import com.huidz.calendar.util.Url;
import com.huidz.calendar.util.ZodiacUtil;
import com.huidz.calendar.view.AdaptionSizeTextView;
import com.huidz.calendar.view.GregorianLunarCalendarView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.huidz.calendar.mainfragment.CalendarFragment.mway;

public class AlmanacFragment extends Fragment implements View.OnClickListener {
    //用于获取视图
    private View root;
    public static String zhishen;
    public RelativeLayout today_rela;
    public static AdaptionSizeTextView huanglitime, huanglitime_text, huanglitime_texts;
    private TextView tianqinum_text;
    /**********************************黄历text************************************/
    public static AdaptionSizeTextView top_one, top_three;
    public static TextView top_two_1, top_two_2, top_two_3, top_two_4, text_jimsg, text_yimsg, xiongshen_text, jishen_text;
    public static TextView taishen_text, wuxing_text, chongsha_text, pengzu_text;
    public static TextView gengzi, niec_day, xinchou, renyan, kuinun, jiaceng, yijitext, bixiong, dingwei, weishen, jijiu, kangmao, xinhai;
    private LinearLayout lookreld, dianji, xingxiu;
    public static TextView titlehuangli;
    private ImageView fenxiang, tianqiimg;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_huangli, container, false);//初始加载
        if (Apiutils.isNetworkConnecteds)
            InitView(root);
        return root;
    }

    String datetime;
    public static String yuegan, rigan, dizhis;
    public static String yuegan1, rigan1, dizhis1;

    /**
     * 初始加载视图
     */
    public void InitView(View view) {
        dianji = view.findViewById(R.id.dianji);
        xingxiu = view.findViewById(R.id.xingxiu);
        xingxiu.setOnClickListener(this);
        top_three = view.findViewById(R.id.top_three);
        dianji.setOnClickListener(this);
        niec_day = view.findViewById(R.id.niec_day);
        lookreld = view.findViewById(R.id.lookreld);
        lookreld.setOnClickListener(this);
        pengzu_text = view.findViewById(R.id.pengzu_text);
        pengzu_text.setOnClickListener(this);
        huanglitime = view.findViewById(R.id.huanglitime);
        huanglitime_text = view.findViewById(R.id.huanglitime_text);
        huanglitime_texts = view.findViewById(R.id.huanglitime_texts);
        fenxiang = view.findViewById(R.id.fenxiang);
        fenxiang.setOnClickListener(this);
        tianqiimg = view.findViewById(R.id.tianqiimg);
        tianqinum_text = view.findViewById(R.id.tianqinum_text);
        titlehuangli = view.findViewById(R.id.titlehuangli);
        titlehuangli.setText(CalendarFragment.timestr);
        titlehuangli.setOnClickListener(this);
        top_one = view.findViewById(R.id.top_one);
        top_two_1 = view.findViewById(R.id.top_two_1);
        top_two_2 = view.findViewById(R.id.top_two_2);
        top_two_3 = view.findViewById(R.id.top_two_3);
        top_two_4 = view.findViewById(R.id.top_two_4);
        text_jimsg = view.findViewById(R.id.text_jimsg);
        text_jimsg.setOnClickListener(this);
        text_yimsg = view.findViewById(R.id.text_yimsg);
        text_yimsg.setOnClickListener(this);
        xiongshen_text = view.findViewById(R.id.xiongshen_text);
        xiongshen_text.setOnClickListener(this);
        jishen_text = view.findViewById(R.id.jishen_text);
        jishen_text.setOnClickListener(this);
        taishen_text = view.findViewById(R.id.taishen_text);
        today_rela = view.findViewById(R.id.today_rela);
        today_rela.setOnClickListener(this);
        taishen_text.setOnClickListener(this);
        wuxing_text = view.findViewById(R.id.wuxing_text);
        wuxing_text.setOnClickListener(this);
        chongsha_text = view.findViewById(R.id.chongsha_text);
        chongsha_text.setOnClickListener(this);
        gengzi = view.findViewById(R.id.gengzi);
        xinchou = view.findViewById(R.id.xinchou);
        renyan = view.findViewById(R.id.renyan);
        kuinun = view.findViewById(R.id.kuinun);
        jiaceng = view.findViewById(R.id.jiaceng);
        yijitext = view.findViewById(R.id.yiji);
        bixiong = view.findViewById(R.id.bixiong);
        dingwei = view.findViewById(R.id.dingwei);
        weishen = view.findViewById(R.id.weishen);
        jijiu = view.findViewById(R.id.jijiu);
        kangmao = view.findViewById(R.id.kangmao);
        xinhai = view.findViewById(R.id.xinhai);
        String yiji = LunarCalendar.getyiji(CalendarFragment.mCalendarView.getCurYear(), CalendarFragment.mCalendarView.getCurMonth(), CalendarFragment.mCalendarView.getCurDay());
        File file = new File(getContext().getFilesDir(), "saa.db");
        database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
        if (CalendarFragment.mCalendarView.getCurMonth() > 9 && CalendarFragment.mCalendarView.getCurDay() > 9) {
            datetime = CalendarFragment.mCalendarView.getCurYear() + "-" + CalendarFragment.mCalendarView.getCurMonth() + "-" + CalendarFragment.mCalendarView.getCurDay();
        } else if (CalendarFragment.mCalendarView.getCurMonth() > 9 && CalendarFragment.mCalendarView.getCurDay() <= 9) {
            datetime = CalendarFragment.mCalendarView.getCurYear() + "-" + CalendarFragment.mCalendarView.getCurMonth() + "-" + ("0" + CalendarFragment.mCalendarView.getCurDay());
        } else if (CalendarFragment.mCalendarView.getCurMonth() <= 9 && CalendarFragment.mCalendarView.getCurDay() <= 9) {
            datetime = CalendarFragment.mCalendarView.getCurYear() + "-" + ("0" + CalendarFragment.mCalendarView.getCurMonth()) + "-" + ("0" + CalendarFragment.mCalendarView.getCurDay());
        } else {
            datetime = CalendarFragment.mCalendarView.getCurYear() + "-" + ("0" + CalendarFragment.mCalendarView.getCurMonth()) + "-" + CalendarFragment.mCalendarView.getCurDay();
        }
        huanglitime.setText(CalendarFragment.mCalendarView.getCurDay() + "");
        String s = LunarCalendar.getLunarString(CalendarFragment.mCalendarView.getCurYear(), CalendarFragment.mCalendarView.getCurMonth(), CalendarFragment.mCalendarView.getCurDay());
        String almanac = s.substring(0, 5);
        String niceday = s.substring(5, s.length());
        niec_day.setText(niceday);
        huanglitime_text.setText(almanac + " 星期" + Apiutils.GetWeekday(mway));
        ganzhi = LunarCalendar.cyclical(CalendarFragment.mCalendarView.getCurYear(), CalendarFragment.mCalendarView.getCurMonth(), CalendarFragment.mCalendarView.getCurDay());
        huanglitimessstr = Apiutils.Niantg + "年 " + Apiutils.Yuetg + "月 " + Apiutils.Ritg + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(CalendarFragment.mCalendarView.getCurYear()) + "」";
        yuegan = Apiutils.Yuetg.substring(1, 2);
        rigan = Apiutils.Ritg.substring(1, 2);
        dizhis = Apiutils.Ritg.substring(0, 1);
        SelectCode(datetime);
        GetGanZhi(datetime);
        SelectYiJi(gz + "", jx + "");
        int code = (CalendarFragment.mCalendarView.getCurMonth() + 10) % 12 + 1;
        SelectTaiShen(code + "", Apiutils.Ritg);
        top_three.setText(LunarCalendar.Getxingxiu(datetime));
        huanglitime_texts.setText(huanglitimessstr);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        String datetime = simpleDateFormat.format(date);
        int y = Integer.parseInt(datetime.substring(0, 4));
//        String s4 = TianGanDizhiShengXiao.getTGDZName(y);
        for (int g = 0; g < LunchActivity.jsonModel.getNewslist().size(); g++) {
            if (LunchActivity.jsonModel.getNewslist().get(g).getGanzhi().equals(ganzhis)) {
                chongsha_text.setText(LunchActivity.jsonModel.getNewslist().get(g).getChongsha());
                Apiutils.ChongSha = LunchActivity.jsonModel.getNewslist().get(g).getChongsha();
                Apiutils.ShengXiao = LunchActivity.jsonModel.getNewslist().get(g).getChongsha().substring(1, 2);
                Apiutils.FangWei = LunchActivity.jsonModel.getNewslist().get(g).getChongsha().substring(4, 5);
                Apiutils.GET_TAISHEN = LunchActivity.jsonModel.getNewslist().get(g).getTaishen();
                wuxing_text.setText(LunchActivity.jsonModel.getNewslist().get(g).getName());
                Apiutils.WuXing = LunchActivity.jsonModel.getNewslist().get(g).getName();
                xiongji = LunchActivity.jsonModel.getNewslist().get(g).getShijian();
                xiongjiary = xiongji.split("[|]");
                taishen_text.setText(Apiutils.GET_TAISHEN);
                top_two_1.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(0, 2));
                top_two_2.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(3, 5));
                top_two_3.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(6, 8));
                top_two_4.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(3, 5));
                break;
            }
        }
        if (xiongjiary != null) {
            gengzi.setText("庚子" + xiongjiary[0]);
            xinchou.setText("辛丑" + xiongjiary[1]);
            renyan.setText("壬寅" + xiongjiary[2]);
            kuinun.setText("癸卯" + xiongjiary[3]);
            jiaceng.setText("甲辰" + xiongjiary[4]);
            yijitext.setText("乙巳" + xiongjiary[5]);
            bixiong.setText("丙午" + xiongjiary[6]);
            dingwei.setText("丁未" + xiongjiary[7]);
            weishen.setText("戊申" + xiongjiary[8]);
            jijiu.setText("己酉" + xiongjiary[9]);
            kangmao.setText("庚戌" + xiongjiary[10]);
            xinhai.setText("辛亥" + xiongjiary[11]);
            settextcolor(LunchActivity.colorid);
        }
    }

    public static String xiongji;
    public static String[] xiongjiary;

    /**
     * 修改时辰
     *
     * @param colorid
     */
    public void settextcolor(int colorid) {
        switch (colorid) {
            case 0:
                gengzi.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 1:
                xinchou.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 2:
                renyan.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 3:
                kuinun.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 4:
                jiaceng.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 5:
                yijitext.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 6:
                bixiong.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 7:
                dingwei.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 8:
                weishen.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 9:
                jijiu.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 10:
                kangmao.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case 11:
                xinhai.setTextColor(getResources().getColor(R.color.title_color));
                break;
        }
    }

    private String ganzhi;
    public static String WUXING;

    /**
     * 获取宜忌的方法
     */
    public static void SelectYiJi(String gz, String jx) {
        Cursor mCursor = database.query("YJData", null, "jx=? and gz=?", new String[]{jx, gz}, null,
                null, null);
        while (mCursor.moveToNext()) {
            yi = mCursor.getString(3);
            text_yimsg.setText(yi);
            ji = mCursor.getString(2);
            text_jimsg.setText(ji);
            strarray = yi.split("[ ]");
            strarrays = ji.split("[ ]");
        }
    }

    public static String[] strarray, strarrays, jishen, xiongshen;

    public static String ganzhinums = "", jxstr = "";

    /**
     * 获取胎神等数据
     *
     * @param AutoCode 传入的时间
     */
    public static void SelectTaiShen(String AutoCode, String dayGz) {
        Cursor mCursor = database.query("advices", null, "Code=? and dayGz=?", new String[]{AutoCode, dayGz}, null,
                null, null);
        while (mCursor.moveToNext()) {
            jishen_text.setText(mCursor.getString(4));
            xiongshen_text.setText(mCursor.getString(5));
            Apiutils.GET_XIONGSHEN = mCursor.getString(5);
            Apiutils.GET_JISHEN = mCursor.getString(4);
            ganzhis = Apiutils.Ritg;
            jishen = mCursor.getString(4).split("[ ]");
            xiongshen = mCursor.getString(5).split("[ ]");
        }
    }

    /**
     * 获取胎神等数据
     *
     * @param AutoCode 传入的时间
     */
    public static void SelectTaiShen(String AutoCode, String dayGz,String sos) {
        Cursor mCursor = database.query("advices", null, "Code=? and dayGz=?", new String[]{AutoCode, dayGz}, null,
                null, null);
        while (mCursor.moveToNext()) {
            jishen_text.setText(mCursor.getString(4));
            xiongshen_text.setText(mCursor.getString(5));
            Apiutils.GET_XIONGSHEN = mCursor.getString(5);
            Apiutils.GET_JISHEN = mCursor.getString(4);
            ganzhis = Apiutils.Ritg1;
            jishen = mCursor.getString(4).split("[ ]");
            xiongshen = mCursor.getString(5).split("[ ]");
        }
    }

    /**
     * 获取干支等数据
     *
     * @param dayGz 传入的时间
     */
    public static void GetGanZhi(String dayGz) {
        Cursor mCursor = database.query("IndexTable", null, "_Date=?", new String[]{dayGz}, null,
                null, null);
        while (mCursor.moveToNext()) {
            ganzhinums = mCursor.getString(2);
            jxstr = mCursor.getString(1);
        }
    }

    public static String ganzhis;
    public static int ms = 0, ds = 0, dz = 0;

    /**
     * 获取时间的code
     *
     * @param _Date 传入时间
     */
    public static void SelectCode(String _Date) {
        Cursor mCursor = database.query("IndexTable", null, "_Date=?", new String[]{_Date}, null,
                null, null);
        while (mCursor.moveToNext()) {
            jx = Integer.parseInt(mCursor.getString(1));
            gz = Integer.parseInt(mCursor.getString(2));
            for (int m = 0; m < LunarCalendar.Zhi.length; m++) {
                if (LunarCalendar.Zhi[m].equals(yuegan)) {
                    ms = m;
                    break;
                }
            }
            for (int d = 0; d < LunarCalendar.Zhi.length; d++) {
                if (LunarCalendar.Zhi[d].equals(rigan)) {
                    ds = d;
                    break;
                }
            }
            for (int ds = 0; ds < LunarCalendar.Gan.length; ds++) {
                if (LunarCalendar.Gan[ds].equals(dizhis)) {
                    dz = ds;
                    break;
                }
            }
            Apiutils.GET_ZHISHEN = LunarCalendar.GetZhishen(ms, ds);
            top_one.setText(LunarCalendar.GetZhishen(ms, ds));
            zhishen = SelectXDW(LunarCalendar.GetZhishen(ms, ds));
            pengzu_text.setText(LunarCalendar.PengZu(dz, ds));
        }
    }

    public static void SelectCode(String _Date, String str) {
        Cursor mCursor = database.query("IndexTable", null, "_Date=?", new String[]{_Date}, null,
                null, null);
        while (mCursor.moveToNext()) {
            jx = Integer.parseInt(mCursor.getString(1));
            gz = Integer.parseInt(mCursor.getString(2));
            for (int m = 0; m < LunarCalendar.Zhi.length; m++) {
                if (LunarCalendar.Zhi[m].equals(yuegan1)) {
                    ms = m;
                    break;
                }
            }
            for (int d = 0; d < LunarCalendar.Zhi.length; d++) {
                if (LunarCalendar.Zhi[d].equals(rigan1)) {
                    ds = d;
                    break;
                }
            }
            for (int ds = 0; ds < LunarCalendar.Gan.length; ds++) {
                if (LunarCalendar.Gan[ds].equals(dizhis1)) {
                    dz = ds;
                    break;
                }
            }
            Apiutils.GET_ZHISHEN = LunarCalendar.GetZhishen(ms, ds);
            top_one.setText(LunarCalendar.GetZhishen(ms, ds));
            zhishen = SelectXDW(LunarCalendar.GetZhishen(ms, ds));
            pengzu_text.setText(LunarCalendar.PengZu(dz, ds));
        }
    }


    /**
     * 获取现代文的方法
     *
     * @param ancient 条件查询值
     */
    public static String SelectXDW(String ancient) {
        Cursor mCursor = database.query("explain", null, "ancient=?", new String[]{ancient}, null,
                null, null);
        String getxdw = null;
        while (mCursor.moveToNext()) {
            getxdw = mCursor.getString(1);
        }
        return getxdw;
    }

    public static int jx, gz;
    public static SQLiteDatabase database;
    public static String yi, ji;
    private int yiindex = 1, jiindex = 2;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fenxiang:

                break;
            case R.id.titlehuangli:
                window(getActivity());
                break;
            case R.id.today_rela:
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String timestr;
                Date gettime = new Date(System.currentTimeMillis());
                timestr = simpleDateFormat.format(gettime);
                SelectCode(timestr, "1");
                MyDate.initGanZhi(Integer.parseInt(timestr.substring(0, 4)), Integer.parseInt(timestr.substring(5, 7)), Integer.parseInt(timestr.substring(8, 10)), "1");
                yuegan1 = Apiutils.Yuetg1.substring(1, 2);
                rigan1 = Apiutils.Ritg1.substring(1, 2);
                dizhis1 = Apiutils.Ritg1.substring(0, 1);
                String s = LunarCalendar.getLunarString(Integer.parseInt(timestr.substring(0, 4)), Integer.parseInt(timestr.substring(5, 7)), Integer.parseInt(timestr.substring(8, 10)));
                home(timestr, Integer.parseInt(timestr.substring(5, 7)), Integer.parseInt(timestr.substring(8, 10)), s, Apiutils.Niantg1 + "年 " + Apiutils.Yuetg1 + "月 " + Apiutils.Ritg1 + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(Integer.parseInt(timestr.substring(0, 4))) + "」", s + " 星期" + Apiutils.GetWeekday(mway), Integer.parseInt(timestr.substring(0, 4)) + "年" + Integer.parseInt(timestr.substring(5, 7)) + "月",false);
                break;
            case R.id.wuxing_text:
                Intent wuxing = new Intent(getActivity(), HuangliXdwActivity.class);
                wuxing.putExtra("type", "1");
                wuxing.putExtra("listloca", "wuxing");
                startActivity(wuxing, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.taishen_text:
                Intent taishen = new Intent(getActivity(), HuangliXdwActivity.class);
                taishen.putExtra("type", "1");
                taishen.putExtra("listloca", "taishen");
                startActivity(taishen, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.xiongshen_text:
                Intent xiongshen = new Intent(getActivity(), HuangliXdwActivity.class);
                xiongshen.putExtra("type", "1");
                xiongshen.putExtra("listloca", "xiongshen");
                startActivity(xiongshen, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.jishen_text:
                Intent jishen = new Intent(getActivity(), HuangliXdwActivity.class);
                jishen.putExtra("type", "1");
                jishen.putExtra("listloca", "jishen");
                startActivity(jishen, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.chongsha_text:
                Intent tocs = new Intent(getActivity(), HuangliXdwActivity.class);
                tocs.putExtra("type", "1");
                tocs.putExtra("listloca", "chongsha");
                startActivity(tocs, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.xingxiu:
                Intent toxx = new Intent(getActivity(), HuangliXdwActivity.class);
                toxx.putExtra("type", "1");
                toxx.putExtra("listloca", "xingxiu");
                startActivity(toxx, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.lookreld:
                Intent toxdw = new Intent(getActivity(), HuangliXdwActivity.class);
                toxdw.putExtra("type", "0");
                startActivity(toxdw, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.dianji:
//                Intent tohl = new Intent(getActivity(), HuangliActivity.class);
//                startActivity(tohl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());

                Intent tohl = new Intent(getActivity(), HuangliXdwActivity.class);
                tohl.putExtra("type", "1");
                tohl.putExtra("listloca", "zhishen");
                startActivity(tohl, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.text_yimsg:
                Intent toxdwyi = new Intent(getActivity(), HuangliXdwActivity.class);
                toxdwyi.putExtra("type", "1");
                toxdwyi.putExtra("listloca", "yi");
                startActivity(toxdwyi, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.text_jimsg:
                Intent toxdwji = new Intent(getActivity(), HuangliXdwActivity.class);
                toxdwji.putExtra("type", "1");
                toxdwji.putExtra("listloca", "ji");
                startActivity(toxdwji, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
            case R.id.pengzu_text:
                Intent pz = new Intent(getActivity(), HuangliXdwActivity.class);
                pz.putExtra("type", "1");
                pz.putExtra("listloca", "pz");
                startActivity(pz, ActivityOptions.makeSceneTransitionAnimation(getActivity()).toBundle());
                break;
        }
    }

    public static String huanglitimessstr;

    public static void home(String time, int month, int day, String s, String name, String week, String ny, boolean sos) {
        if (sos) {
            yuegan = Apiutils.Yuetg.substring(1, 2);
            rigan = Apiutils.Ritg.substring(1, 2);
            dizhis = Apiutils.Ritg.substring(0, 1);
            SelectCode(time);
        } else {
            yuegan = Apiutils.Yuetg1.substring(1, 2);
            rigan = Apiutils.Ritg1.substring(1, 2);
            dizhis = Apiutils.Ritg1.substring(0, 1);
            SelectCode(time, "1");
        }
        GetGanZhi(time);
        SelectYiJi(gz + "", jx + "");
        int code = (month + 10) % 12 + 1;
        if (sos) {
            SelectTaiShen(code + "", Apiutils.Ritg);
        } else {
            SelectTaiShen(code + "", Apiutils.Ritg1,"");
        }
        huanglitime.setText(day + "");
        String niceday = s.substring(5, s.length());
        huanglitime_texts.setText(name);
        huanglitime_text.setText(week);
        niec_day.setText(niceday);
        titlehuangli.setText(ny);
        top_three.setText(LunarCalendar.Getxingxiu(time));
        for (int g = 0; g < LunchActivity.jsonModel.getNewslist().size(); g++) {
            if (LunchActivity.jsonModel.getNewslist().get(g).getGanzhi().equals(ganzhis)) {
                chongsha_text.setText(LunchActivity.jsonModel.getNewslist().get(g).getChongsha());
                Apiutils.ChongSha = LunchActivity.jsonModel.getNewslist().get(g).getChongsha();
                Apiutils.ShengXiao = LunchActivity.jsonModel.getNewslist().get(g).getChongsha().substring(1, 2);
                Apiutils.FangWei = LunchActivity.jsonModel.getNewslist().get(g).getChongsha().substring(4, 5);
                Apiutils.GET_TAISHEN = LunchActivity.jsonModel.getNewslist().get(g).getTaishen();
                wuxing_text.setText(LunchActivity.jsonModel.getNewslist().get(g).getName());
                Apiutils.WuXing = LunchActivity.jsonModel.getNewslist().get(g).getName();
                xiongji = LunchActivity.jsonModel.getNewslist().get(g).getShijian();
                xiongjiary = xiongji.split("[|]");
                taishen_text.setText(Apiutils.GET_TAISHEN);
                top_two_1.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(0, 2));
                top_two_2.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(3, 5));
                top_two_3.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(6, 8));
                top_two_4.setText(LunchActivity.jsonModel.getNewslist().get(g).getFangwei().substring(3, 5));
                break;
            }
        }
        if (xiongjiary != null) {
            gengzi.setText("庚子" + xiongjiary[0]);
            xinchou.setText("辛丑" + xiongjiary[1]);
            renyan.setText("壬寅" + xiongjiary[2]);
            kuinun.setText("癸卯" + xiongjiary[3]);
            jiaceng.setText("甲辰" + xiongjiary[4]);
            yijitext.setText("乙巳" + xiongjiary[5]);
            bixiong.setText("丙午" + xiongjiary[6]);
            dingwei.setText("丁未" + xiongjiary[7]);
            weishen.setText("戊申" + xiongjiary[8]);
            jijiu.setText("己酉" + xiongjiary[9]);
            kangmao.setText("庚戌" + xiongjiary[10]);
            xinhai.setText("辛亥" + xiongjiary[11]);
        }
    }

    private PopupWindow popupWindow;
    GregorianLunarCalendarView mGLCView;
    private RelativeLayout Gregorian, getLunar;
    private LinearLayout waiguang;
    private TextView Gregoriantext, getLunartext;
    private int getcalendar = 0;//0为公历,默认情况下选择是公历
    private TextView title_text, get_title_text;
    int y;
    int m;
    int d;
    java.util.Calendar calendars;

    public void window(Context context) {
        View popupwindowview = LayoutInflater.from(context).inflate(
                R.layout.seleced_calendar, null);
        mGLCView = popupwindowview.findViewById(R.id.calendar_view);
        //采用GregorianLunarCalendarView.CalendarData内部类来存储当前日期，使用getCalendarData()函数获取选中date数据
        GregorianLunarCalendarView.CalendarData calendarData = mGLCView.getCalendarData();
        mGLCView.init();
        waiguang = popupwindowview.findViewById(R.id.waiguang);
        waiguang.setVisibility(View.GONE);
        Gregorian = popupwindowview.findViewById(R.id.Gregorian);
        getLunar = popupwindowview.findViewById(R.id.getLunar);
        Gregoriantext = popupwindowview.findViewById(R.id.Gregoriantext);
        getLunartext = popupwindowview.findViewById(R.id.getLunartext);
        get_title_text = popupwindowview.findViewById(R.id.get_title_text);
        title_text = popupwindowview.findViewById(R.id.title_text);
        title_text.setText(R.string.msgtext);
        Gregorian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                title_text.setText(R.string.msgtext);
                getcalendar = 0;
                Gregorian.setBackgroundDrawable(getResources().getDrawable(R.drawable.rili_select_rela));
                getLunar.setBackgroundDrawable(getResources().getDrawable(R.color.colorwhite50));
                Gregoriantext.setTextColor(getResources().getColor(R.color.colorwhite));
                getLunartext.setTextColor(getResources().getColor(R.color.title_color));
                get_title_text.setText("");
            }
        });
        getLunar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                title_text.setText(R.string.msgtext1);
                getcalendar = 1;
                Gregorian.setBackgroundDrawable(getResources().getDrawable(R.color.colorwhite50));
                getLunar.setBackgroundDrawable(getResources().getDrawable(R.drawable.rili_select_rela));
                Gregoriantext.setTextColor(getResources().getColor(R.color.title_color));
                getLunartext.setTextColor(getResources().getColor(R.color.colorwhite));
                get_title_text.setText("");
            }
        });
        RelativeLayout close_rela = popupwindowview.findViewById(R.id.close_rela);
        close_rela.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        mGLCView.setOnDateChangedListener(new GregorianLunarCalendarView.OnDateChangedListener() {
            @Override
            public void onDateChanged(GregorianLunarCalendarView.CalendarData calendarData) {
                java.util.Calendar calendars = calendarData.getCalendar();
                String text_msg;
                y = calendars.get(java.util.Calendar.YEAR);
                m = calendars.get(java.util.Calendar.MONTH) + 1;
                d = calendars.get(java.util.Calendar.DAY_OF_MONTH);
//                text_msg = "公历";
//                get_title_text.setText("当前对应的" + text_msg + ":" + y + "年" + m + "月" + d + "日");
            }
        });
        TextView is_today = popupwindowview.findViewById(R.id.is_today);
        is_today.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mGLCView.init();
            }
        });
        RelativeLayout ok_rela = popupwindowview.findViewById(R.id.ok_rela);
        ok_rela.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GregorianLunarCalendarView.CalendarData calendarData = mGLCView.getCalendarData();
                calendars = calendarData.getCalendar();
                int y = calendars.get(java.util.Calendar.YEAR);
                int m = calendars.get(java.util.Calendar.MONTH) + 1;
                int d = calendars.get(java.util.Calendar.DAY_OF_MONTH);
                if ((calendars.get(java.util.Calendar.MONTH) + 1) > 9 && calendars.get(java.util.Calendar.DAY_OF_MONTH) > 9) {
                    int week = calendars.DAY_OF_WEEK - 1;
                    SelectCode(y + "-" + m + " -" + d, "1");
                    MyDate.initGanZhi(y, m, d, "1");
                    yuegan1 = Apiutils.Yuetg1.substring(1, 2);
                    rigan1 = Apiutils.Ritg1.substring(1, 2);
                    dizhis1 = Apiutils.Ritg1.substring(0, 1);
                    String s = LunarCalendar.getLunarString(y, m, d);
                    home(y + "-" + m + " -" + d, m, d, s, Apiutils.Niantg1 + "年 " + Apiutils.Yuetg1 + "月 " + Apiutils.Ritg1 + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(y) + "」", s + " 星期" + Apiutils.GetWeekday(week), y + "年" + m + "月",false);
                } else if ((calendars.get(java.util.Calendar.MONTH) + 1) > 9 && calendars.get(java.util.Calendar.DAY_OF_MONTH) <= 9) {
                    int week = calendars.DAY_OF_WEEK - 1;
                    SelectCode(y + "-" + m + ("-0" + d), "1");
                    MyDate.initGanZhi(y, m, d, "1");
                    yuegan1 = Apiutils.Yuetg1.substring(1, 2);
                    rigan1 = Apiutils.Ritg1.substring(1, 2);
                    dizhis1 = Apiutils.Ritg1.substring(0, 1);
                    String s = LunarCalendar.getLunarString(y, m, d);
                    home(y + "-" + m + ("-0" + d), m, d, s, Apiutils.Niantg1 + "年 " + Apiutils.Yuetg1 + "月 " + Apiutils.Ritg1 + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(y) + "」", s + " 星期" + Apiutils.GetWeekday(week), y + "年" + m + "月",false);
                } else if ((calendars.get(java.util.Calendar.MONTH) + 1) <= 9 && calendars.get(java.util.Calendar.DAY_OF_MONTH) <= 9) {
                    int week = calendars.DAY_OF_WEEK - 1;
                    SelectCode(y + "-0" + m + "-0" + d, "1");
                    MyDate.initGanZhi(y, m, d, "1");
                    yuegan1 = Apiutils.Yuetg1.substring(1, 2);
                    rigan1 = Apiutils.Ritg1.substring(1, 2);
                    dizhis1 = Apiutils.Ritg1.substring(0, 1);
                    String s = LunarCalendar.getLunarString(y, m, d);
                    home(y + "-0" + m + "-0" + d, m, d, s, Apiutils.Niantg1 + "年 " + Apiutils.Yuetg1 + "月 " + Apiutils.Ritg1 + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(y) + "」", s + " 星期" + Apiutils.GetWeekday(week), y + "年" + m + "月",false);
                } else {
                    int week = calendars.DAY_OF_WEEK - 1;
                    SelectCode(y + "-0" + m + d + "", "1");
                    MyDate.initGanZhi(y, m, d, "1");
                    yuegan1 = Apiutils.Yuetg1.substring(1, 2);
                    rigan1 = Apiutils.Ritg1.substring(1, 2);
                    dizhis1 = Apiutils.Ritg1.substring(0, 1);
                    String s = LunarCalendar.getLunarString(y, m, d);
                    home(y + "-0" + m + d + "", m, d, s, Apiutils.Niantg1 + "年 " + Apiutils.Yuetg1 + "月 " + Apiutils.Ritg1 + "日 「属" + TianGanDizhiShengXiao.getAnimalYearName(y) + "」", s + " 星期" + Apiutils.GetWeekday(week), y + "年" + m + "月",false);
                }
                popupWindow.dismiss();
            }
        });
        popupWindow = new
                PopupWindow(popupwindowview, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 外部点击事件
        popupWindow.setTouchable(true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
                lp.alpha = 1f;
                getActivity().getWindow().setAttributes(lp);
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = 0.4f;
        getActivity().
                getWindow().
                setAttributes(lp);
        popupWindow.showAtLocation(huanglitime, Gravity.CENTER, 10, 10);
    }
}